
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 03/16/2011 22:27:07
-- Generated from EDMX file: D:\Documents\Visual Studio 2010\Projects\BookID\XPressify.Data\DataModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [XPressify];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_BaseObjectPostAssoc]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseMessages_Post] DROP CONSTRAINT [FK_BaseObjectPostAssoc];
GO
IF OBJECT_ID(N'[dbo].[FK_PostPostLikeAssoc]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseMessages_PostLike] DROP CONSTRAINT [FK_PostPostLikeAssoc];
GO
IF OBJECT_ID(N'[dbo].[FK_ThreadMessageAssoc]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseMessages_Message] DROP CONSTRAINT [FK_ThreadMessageAssoc];
GO
IF OBJECT_ID(N'[dbo].[FK_BaseSubjectBaseMessageAssoc]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseMessages] DROP CONSTRAINT [FK_BaseSubjectBaseMessageAssoc];
GO
IF OBJECT_ID(N'[dbo].[FK_AlbumMediaAssoc]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseObjects_Media] DROP CONSTRAINT [FK_AlbumMediaAssoc];
GO
IF OBJECT_ID(N'[dbo].[FK_BaseSubjectRelationship]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseObjects_Relationship] DROP CONSTRAINT [FK_BaseSubjectRelationship];
GO
IF OBJECT_ID(N'[dbo].[FK_BaseSubjectBaseObjectAssoc]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseObjects] DROP CONSTRAINT [FK_BaseSubjectBaseObjectAssoc];
GO
IF OBJECT_ID(N'[dbo].[FK_BaseSubjectThreadParticipantAssoc]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ThreadParticipants] DROP CONSTRAINT [FK_BaseSubjectThreadParticipantAssoc];
GO
IF OBJECT_ID(N'[dbo].[FK_ThreadThreadParticipantAssoc]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ThreadParticipants] DROP CONSTRAINT [FK_ThreadThreadParticipantAssoc];
GO
IF OBJECT_ID(N'[dbo].[FK_PostPostAssoc]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseMessages_Post] DROP CONSTRAINT [FK_PostPostAssoc];
GO
IF OBJECT_ID(N'[dbo].[FK_PostCreator]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseMessages_Post] DROP CONSTRAINT [FK_PostCreator];
GO
IF OBJECT_ID(N'[dbo].[FK_Post_inherits_BaseMessage]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseMessages_Post] DROP CONSTRAINT [FK_Post_inherits_BaseMessage];
GO
IF OBJECT_ID(N'[dbo].[FK_PostLike_inherits_BaseMessage]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseMessages_PostLike] DROP CONSTRAINT [FK_PostLike_inherits_BaseMessage];
GO
IF OBJECT_ID(N'[dbo].[FK_Thread_inherits_BaseMessage]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseMessages_Thread] DROP CONSTRAINT [FK_Thread_inherits_BaseMessage];
GO
IF OBJECT_ID(N'[dbo].[FK_Message_inherits_BaseMessage]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseMessages_Message] DROP CONSTRAINT [FK_Message_inherits_BaseMessage];
GO
IF OBJECT_ID(N'[dbo].[FK_Album_inherits_BaseObject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseObjects_Album] DROP CONSTRAINT [FK_Album_inherits_BaseObject];
GO
IF OBJECT_ID(N'[dbo].[FK_Media_inherits_BaseObject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseObjects_Media] DROP CONSTRAINT [FK_Media_inherits_BaseObject];
GO
IF OBJECT_ID(N'[dbo].[FK_Relationship_inherits_BaseObject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseObjects_Relationship] DROP CONSTRAINT [FK_Relationship_inherits_BaseObject];
GO
IF OBJECT_ID(N'[dbo].[FK_Status_inherits_BaseObject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseObjects_Status] DROP CONSTRAINT [FK_Status_inherits_BaseObject];
GO
IF OBJECT_ID(N'[dbo].[FK_Link_inherits_BaseObject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseObjects_Link] DROP CONSTRAINT [FK_Link_inherits_BaseObject];
GO
IF OBJECT_ID(N'[dbo].[FK_Note_inherits_BaseObject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseObjects_Note] DROP CONSTRAINT [FK_Note_inherits_BaseObject];
GO
IF OBJECT_ID(N'[dbo].[FK_User_inherits_BaseSubject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseSubjects_User] DROP CONSTRAINT [FK_User_inherits_BaseSubject];
GO
IF OBJECT_ID(N'[dbo].[FK_Group_inherits_BaseSubject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseSubjects_Group] DROP CONSTRAINT [FK_Group_inherits_BaseSubject];
GO
IF OBJECT_ID(N'[dbo].[FK_Event_inherits_BaseSubject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseSubjects_Event] DROP CONSTRAINT [FK_Event_inherits_BaseSubject];
GO
IF OBJECT_ID(N'[dbo].[FK_Notification_inherits_BaseMessage]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseMessages_Notification] DROP CONSTRAINT [FK_Notification_inherits_BaseMessage];
GO
IF OBJECT_ID(N'[dbo].[FK_Page_inherits_BaseSubject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseSubjects_Page] DROP CONSTRAINT [FK_Page_inherits_BaseSubject];
GO
IF OBJECT_ID(N'[dbo].[FK_Application_inherits_BaseSubject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BaseSubjects_Application] DROP CONSTRAINT [FK_Application_inherits_BaseSubject];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[BaseObjects]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BaseObjects];
GO
IF OBJECT_ID(N'[dbo].[BaseSubjects]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BaseSubjects];
GO
IF OBJECT_ID(N'[dbo].[BaseMessages]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BaseMessages];
GO
IF OBJECT_ID(N'[dbo].[ThreadParticipants]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ThreadParticipants];
GO
IF OBJECT_ID(N'[dbo].[BaseMessages_Post]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BaseMessages_Post];
GO
IF OBJECT_ID(N'[dbo].[BaseMessages_PostLike]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BaseMessages_PostLike];
GO
IF OBJECT_ID(N'[dbo].[BaseMessages_Thread]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BaseMessages_Thread];
GO
IF OBJECT_ID(N'[dbo].[BaseMessages_Message]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BaseMessages_Message];
GO
IF OBJECT_ID(N'[dbo].[BaseObjects_Album]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BaseObjects_Album];
GO
IF OBJECT_ID(N'[dbo].[BaseObjects_Media]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BaseObjects_Media];
GO
IF OBJECT_ID(N'[dbo].[BaseObjects_Relationship]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BaseObjects_Relationship];
GO
IF OBJECT_ID(N'[dbo].[BaseObjects_Status]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BaseObjects_Status];
GO
IF OBJECT_ID(N'[dbo].[BaseObjects_Link]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BaseObjects_Link];
GO
IF OBJECT_ID(N'[dbo].[BaseObjects_Note]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BaseObjects_Note];
GO
IF OBJECT_ID(N'[dbo].[BaseSubjects_User]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BaseSubjects_User];
GO
IF OBJECT_ID(N'[dbo].[BaseSubjects_Group]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BaseSubjects_Group];
GO
IF OBJECT_ID(N'[dbo].[BaseSubjects_Event]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BaseSubjects_Event];
GO
IF OBJECT_ID(N'[dbo].[BaseMessages_Notification]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BaseMessages_Notification];
GO
IF OBJECT_ID(N'[dbo].[BaseSubjects_Page]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BaseSubjects_Page];
GO
IF OBJECT_ID(N'[dbo].[BaseSubjects_Application]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BaseSubjects_Application];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'BaseObjects'
CREATE TABLE [dbo].[BaseObjects] (
    [Id] uniqueidentifier  NOT NULL,
    [Created] datetime  NOT NULL,
    [Type] smallint  NOT NULL,
    [Name] nvarchar(255)  NULL,
    [Description] nvarchar(1024)  NULL,
    [PrivacyType] smallint  NOT NULL,
    [BaseSubjectId] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'BaseSubjects'
CREATE TABLE [dbo].[BaseSubjects] (
    [Id] uniqueidentifier  NOT NULL,
    [Created] datetime  NOT NULL,
    [Type] smallint  NOT NULL,
    [Username] nvarchar(20)  NULL,
    [PrivacyType] smallint  NOT NULL
);
GO

-- Creating table 'BaseMessages'
CREATE TABLE [dbo].[BaseMessages] (
    [Id] uniqueidentifier  NOT NULL,
    [Created] datetime  NOT NULL,
    [Type] smallint  NOT NULL,
    [BaseSubjectId1] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'ThreadParticipants'
CREATE TABLE [dbo].[ThreadParticipants] (
    [Id] uniqueidentifier  NOT NULL,
    [BaseSubjectId] uniqueidentifier  NOT NULL,
    [IsRead] bit  NOT NULL,
    [IsDeleted] bit  NOT NULL,
    [ThreadId] uniqueidentifier  NOT NULL,
    [NewMessage] bit  NOT NULL
);
GO

-- Creating table 'BaseMessages_Post'
CREATE TABLE [dbo].[BaseMessages_Post] (
    [Title] nvarchar(100)  NULL,
    [Description] nvarchar(1024)  NULL,
    [Content] nvarchar(max)  NOT NULL,
    [BaseObjectId] uniqueidentifier  NOT NULL,
    [PostId] uniqueidentifier  NULL,
    [PostType] smallint  NOT NULL,
    [CommentType] smallint  NOT NULL,
    [Id] uniqueidentifier  NOT NULL,
    [Creator_Id] uniqueidentifier  NULL
);
GO

-- Creating table 'BaseMessages_PostLike'
CREATE TABLE [dbo].[BaseMessages_PostLike] (
    [Note] smallint  NOT NULL,
    [PostId] uniqueidentifier  NOT NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'BaseMessages_Thread'
CREATE TABLE [dbo].[BaseMessages_Thread] (
    [Subject] nvarchar(255)  NOT NULL,
    [Read] bit  NOT NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'BaseMessages_Message'
CREATE TABLE [dbo].[BaseMessages_Message] (
    [Body] nvarchar(max)  NOT NULL,
    [ThreadId] uniqueidentifier  NOT NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'BaseObjects_Album'
CREATE TABLE [dbo].[BaseObjects_Album] (
    [Location] nvarchar(255)  NULL,
    [MediaType] smallint  NOT NULL,
    [IsProfile] bit  NOT NULL,
    [IsWall] bit  NOT NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'BaseObjects_Media'
CREATE TABLE [dbo].[BaseObjects_Media] (
    [MediaType] smallint  NOT NULL,
    [FileType] nvarchar(10)  NOT NULL,
    [AlbumId] uniqueidentifier  NOT NULL,
    [IsCover] bit  NOT NULL,
    [IsProfile] bit  NOT NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'BaseObjects_Relationship'
CREATE TABLE [dbo].[BaseObjects_Relationship] (
    [Status] smallint  NOT NULL,
    [RelationType] smallint  NOT NULL,
    [BaseSubjectId2] uniqueidentifier  NOT NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'BaseObjects_Status'
CREATE TABLE [dbo].[BaseObjects_Status] (
    [Content] nvarchar(1024)  NOT NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'BaseObjects_Link'
CREATE TABLE [dbo].[BaseObjects_Link] (
    [Url] nvarchar(max)  NOT NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'BaseObjects_Note'
CREATE TABLE [dbo].[BaseObjects_Note] (
    [Title] nvarchar(1024)  NOT NULL,
    [Content] nvarchar(max)  NOT NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'BaseSubjects_User'
CREATE TABLE [dbo].[BaseSubjects_User] (
    [FirstName] nvarchar(50)  NOT NULL,
    [LastName] nvarchar(100)  NOT NULL,
    [Gender] nvarchar(2)  NOT NULL,
    [Birthday] datetime  NOT NULL,
    [Email] nvarchar(255)  NOT NULL,
    [Password] nvarchar(20)  NOT NULL,
    [Role] nvarchar(10)  NOT NULL,
    [Status] smallint  NOT NULL,
    [LastLogin] datetime  NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'BaseSubjects_Group'
CREATE TABLE [dbo].[BaseSubjects_Group] (
    [Name] nvarchar(255)  NOT NULL,
    [Description] nvarchar(max)  NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'BaseSubjects_Event'
CREATE TABLE [dbo].[BaseSubjects_Event] (
    [StartDate] datetime  NOT NULL,
    [EndDate] datetime  NOT NULL,
    [Location] nvarchar(255)  NOT NULL,
    [Description] nvarchar(max)  NULL,
    [Name] nvarchar(255)  NOT NULL,
    [MembersCanPost] bit  NOT NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'BaseMessages_Notification'
CREATE TABLE [dbo].[BaseMessages_Notification] (
    [Content] nvarchar(max)  NOT NULL,
    [Active] bit  NOT NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'BaseSubjects_Page'
CREATE TABLE [dbo].[BaseSubjects_Page] (
    [Name] nvarchar(255)  NOT NULL,
    [Description] nvarchar(max)  NULL,
    [MembersCanPost] bit  NOT NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'BaseSubjects_Application'
CREATE TABLE [dbo].[BaseSubjects_Application] (
    [Key] uniqueidentifier  NOT NULL,
    [Secret] uniqueidentifier  NOT NULL,
    [Domain] nvarchar(255)  NOT NULL,
    [Url] nvarchar(512)  NOT NULL,
    [ContactEmail] nvarchar(255)  NOT NULL,
    [Name] nvarchar(255)  NOT NULL,
    [Description] nvarchar(1024)  NOT NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'BaseObjects'
ALTER TABLE [dbo].[BaseObjects]
ADD CONSTRAINT [PK_BaseObjects]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'BaseSubjects'
ALTER TABLE [dbo].[BaseSubjects]
ADD CONSTRAINT [PK_BaseSubjects]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'BaseMessages'
ALTER TABLE [dbo].[BaseMessages]
ADD CONSTRAINT [PK_BaseMessages]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'ThreadParticipants'
ALTER TABLE [dbo].[ThreadParticipants]
ADD CONSTRAINT [PK_ThreadParticipants]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'BaseMessages_Post'
ALTER TABLE [dbo].[BaseMessages_Post]
ADD CONSTRAINT [PK_BaseMessages_Post]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'BaseMessages_PostLike'
ALTER TABLE [dbo].[BaseMessages_PostLike]
ADD CONSTRAINT [PK_BaseMessages_PostLike]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'BaseMessages_Thread'
ALTER TABLE [dbo].[BaseMessages_Thread]
ADD CONSTRAINT [PK_BaseMessages_Thread]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'BaseMessages_Message'
ALTER TABLE [dbo].[BaseMessages_Message]
ADD CONSTRAINT [PK_BaseMessages_Message]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'BaseObjects_Album'
ALTER TABLE [dbo].[BaseObjects_Album]
ADD CONSTRAINT [PK_BaseObjects_Album]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'BaseObjects_Media'
ALTER TABLE [dbo].[BaseObjects_Media]
ADD CONSTRAINT [PK_BaseObjects_Media]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'BaseObjects_Relationship'
ALTER TABLE [dbo].[BaseObjects_Relationship]
ADD CONSTRAINT [PK_BaseObjects_Relationship]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'BaseObjects_Status'
ALTER TABLE [dbo].[BaseObjects_Status]
ADD CONSTRAINT [PK_BaseObjects_Status]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'BaseObjects_Link'
ALTER TABLE [dbo].[BaseObjects_Link]
ADD CONSTRAINT [PK_BaseObjects_Link]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'BaseObjects_Note'
ALTER TABLE [dbo].[BaseObjects_Note]
ADD CONSTRAINT [PK_BaseObjects_Note]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'BaseSubjects_User'
ALTER TABLE [dbo].[BaseSubjects_User]
ADD CONSTRAINT [PK_BaseSubjects_User]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'BaseSubjects_Group'
ALTER TABLE [dbo].[BaseSubjects_Group]
ADD CONSTRAINT [PK_BaseSubjects_Group]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'BaseSubjects_Event'
ALTER TABLE [dbo].[BaseSubjects_Event]
ADD CONSTRAINT [PK_BaseSubjects_Event]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'BaseMessages_Notification'
ALTER TABLE [dbo].[BaseMessages_Notification]
ADD CONSTRAINT [PK_BaseMessages_Notification]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'BaseSubjects_Page'
ALTER TABLE [dbo].[BaseSubjects_Page]
ADD CONSTRAINT [PK_BaseSubjects_Page]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'BaseSubjects_Application'
ALTER TABLE [dbo].[BaseSubjects_Application]
ADD CONSTRAINT [PK_BaseSubjects_Application]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [BaseObjectId] in table 'BaseMessages_Post'
ALTER TABLE [dbo].[BaseMessages_Post]
ADD CONSTRAINT [FK_BaseObjectPostAssoc]
    FOREIGN KEY ([BaseObjectId])
    REFERENCES [dbo].[BaseObjects]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BaseObjectPostAssoc'
CREATE INDEX [IX_FK_BaseObjectPostAssoc]
ON [dbo].[BaseMessages_Post]
    ([BaseObjectId]);
GO

-- Creating foreign key on [PostId] in table 'BaseMessages_PostLike'
ALTER TABLE [dbo].[BaseMessages_PostLike]
ADD CONSTRAINT [FK_PostPostLikeAssoc]
    FOREIGN KEY ([PostId])
    REFERENCES [dbo].[BaseMessages_Post]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PostPostLikeAssoc'
CREATE INDEX [IX_FK_PostPostLikeAssoc]
ON [dbo].[BaseMessages_PostLike]
    ([PostId]);
GO

-- Creating foreign key on [ThreadId] in table 'BaseMessages_Message'
ALTER TABLE [dbo].[BaseMessages_Message]
ADD CONSTRAINT [FK_ThreadMessageAssoc]
    FOREIGN KEY ([ThreadId])
    REFERENCES [dbo].[BaseMessages_Thread]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ThreadMessageAssoc'
CREATE INDEX [IX_FK_ThreadMessageAssoc]
ON [dbo].[BaseMessages_Message]
    ([ThreadId]);
GO

-- Creating foreign key on [BaseSubjectId1] in table 'BaseMessages'
ALTER TABLE [dbo].[BaseMessages]
ADD CONSTRAINT [FK_BaseSubjectBaseMessageAssoc]
    FOREIGN KEY ([BaseSubjectId1])
    REFERENCES [dbo].[BaseSubjects]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BaseSubjectBaseMessageAssoc'
CREATE INDEX [IX_FK_BaseSubjectBaseMessageAssoc]
ON [dbo].[BaseMessages]
    ([BaseSubjectId1]);
GO

-- Creating foreign key on [AlbumId] in table 'BaseObjects_Media'
ALTER TABLE [dbo].[BaseObjects_Media]
ADD CONSTRAINT [FK_AlbumMediaAssoc]
    FOREIGN KEY ([AlbumId])
    REFERENCES [dbo].[BaseObjects_Album]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_AlbumMediaAssoc'
CREATE INDEX [IX_FK_AlbumMediaAssoc]
ON [dbo].[BaseObjects_Media]
    ([AlbumId]);
GO

-- Creating foreign key on [BaseSubjectId2] in table 'BaseObjects_Relationship'
ALTER TABLE [dbo].[BaseObjects_Relationship]
ADD CONSTRAINT [FK_BaseSubjectRelationship]
    FOREIGN KEY ([BaseSubjectId2])
    REFERENCES [dbo].[BaseSubjects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BaseSubjectRelationship'
CREATE INDEX [IX_FK_BaseSubjectRelationship]
ON [dbo].[BaseObjects_Relationship]
    ([BaseSubjectId2]);
GO

-- Creating foreign key on [BaseSubjectId] in table 'BaseObjects'
ALTER TABLE [dbo].[BaseObjects]
ADD CONSTRAINT [FK_BaseSubjectBaseObjectAssoc]
    FOREIGN KEY ([BaseSubjectId])
    REFERENCES [dbo].[BaseSubjects]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BaseSubjectBaseObjectAssoc'
CREATE INDEX [IX_FK_BaseSubjectBaseObjectAssoc]
ON [dbo].[BaseObjects]
    ([BaseSubjectId]);
GO

-- Creating foreign key on [BaseSubjectId] in table 'ThreadParticipants'
ALTER TABLE [dbo].[ThreadParticipants]
ADD CONSTRAINT [FK_BaseSubjectThreadParticipantAssoc]
    FOREIGN KEY ([BaseSubjectId])
    REFERENCES [dbo].[BaseSubjects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BaseSubjectThreadParticipantAssoc'
CREATE INDEX [IX_FK_BaseSubjectThreadParticipantAssoc]
ON [dbo].[ThreadParticipants]
    ([BaseSubjectId]);
GO

-- Creating foreign key on [ThreadId] in table 'ThreadParticipants'
ALTER TABLE [dbo].[ThreadParticipants]
ADD CONSTRAINT [FK_ThreadThreadParticipantAssoc]
    FOREIGN KEY ([ThreadId])
    REFERENCES [dbo].[BaseMessages_Thread]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ThreadThreadParticipantAssoc'
CREATE INDEX [IX_FK_ThreadThreadParticipantAssoc]
ON [dbo].[ThreadParticipants]
    ([ThreadId]);
GO

-- Creating foreign key on [PostId] in table 'BaseMessages_Post'
ALTER TABLE [dbo].[BaseMessages_Post]
ADD CONSTRAINT [FK_PostPostAssoc]
    FOREIGN KEY ([PostId])
    REFERENCES [dbo].[BaseMessages_Post]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PostPostAssoc'
CREATE INDEX [IX_FK_PostPostAssoc]
ON [dbo].[BaseMessages_Post]
    ([PostId]);
GO

-- Creating foreign key on [Creator_Id] in table 'BaseMessages_Post'
ALTER TABLE [dbo].[BaseMessages_Post]
ADD CONSTRAINT [FK_PostCreator]
    FOREIGN KEY ([Creator_Id])
    REFERENCES [dbo].[BaseSubjects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PostCreator'
CREATE INDEX [IX_FK_PostCreator]
ON [dbo].[BaseMessages_Post]
    ([Creator_Id]);
GO

-- Creating foreign key on [Id] in table 'BaseMessages_Post'
ALTER TABLE [dbo].[BaseMessages_Post]
ADD CONSTRAINT [FK_Post_inherits_BaseMessage]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[BaseMessages]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'BaseMessages_PostLike'
ALTER TABLE [dbo].[BaseMessages_PostLike]
ADD CONSTRAINT [FK_PostLike_inherits_BaseMessage]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[BaseMessages]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'BaseMessages_Thread'
ALTER TABLE [dbo].[BaseMessages_Thread]
ADD CONSTRAINT [FK_Thread_inherits_BaseMessage]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[BaseMessages]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'BaseMessages_Message'
ALTER TABLE [dbo].[BaseMessages_Message]
ADD CONSTRAINT [FK_Message_inherits_BaseMessage]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[BaseMessages]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'BaseObjects_Album'
ALTER TABLE [dbo].[BaseObjects_Album]
ADD CONSTRAINT [FK_Album_inherits_BaseObject]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[BaseObjects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'BaseObjects_Media'
ALTER TABLE [dbo].[BaseObjects_Media]
ADD CONSTRAINT [FK_Media_inherits_BaseObject]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[BaseObjects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'BaseObjects_Relationship'
ALTER TABLE [dbo].[BaseObjects_Relationship]
ADD CONSTRAINT [FK_Relationship_inherits_BaseObject]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[BaseObjects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'BaseObjects_Status'
ALTER TABLE [dbo].[BaseObjects_Status]
ADD CONSTRAINT [FK_Status_inherits_BaseObject]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[BaseObjects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'BaseObjects_Link'
ALTER TABLE [dbo].[BaseObjects_Link]
ADD CONSTRAINT [FK_Link_inherits_BaseObject]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[BaseObjects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'BaseObjects_Note'
ALTER TABLE [dbo].[BaseObjects_Note]
ADD CONSTRAINT [FK_Note_inherits_BaseObject]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[BaseObjects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'BaseSubjects_User'
ALTER TABLE [dbo].[BaseSubjects_User]
ADD CONSTRAINT [FK_User_inherits_BaseSubject]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[BaseSubjects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'BaseSubjects_Group'
ALTER TABLE [dbo].[BaseSubjects_Group]
ADD CONSTRAINT [FK_Group_inherits_BaseSubject]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[BaseSubjects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'BaseSubjects_Event'
ALTER TABLE [dbo].[BaseSubjects_Event]
ADD CONSTRAINT [FK_Event_inherits_BaseSubject]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[BaseSubjects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'BaseMessages_Notification'
ALTER TABLE [dbo].[BaseMessages_Notification]
ADD CONSTRAINT [FK_Notification_inherits_BaseMessage]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[BaseMessages]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'BaseSubjects_Page'
ALTER TABLE [dbo].[BaseSubjects_Page]
ADD CONSTRAINT [FK_Page_inherits_BaseSubject]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[BaseSubjects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'BaseSubjects_Application'
ALTER TABLE [dbo].[BaseSubjects_Application]
ADD CONSTRAINT [FK_Application_inherits_BaseSubject]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[BaseSubjects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------